%adjacency matrix for graph representation
N = 12;

names = {'SMOKING', 'YELLOW_FINGERS', 'ANXIETY', 'PEER_PRESSURE', ...
              'GENETICS', 'ATTENTION_DISORDER', 'BORN_ON_EVEN_DAY', ...
              'CAR_ACCIDENT', 'FATIGUE', 'ALLERGY', 'COUGHING', 'CANCER'};

dag = zeros(N,N);
SMOKING = 1;
YELLOW_FINGERS = 2;
ANXIETY = 3;
PEER_PRESSURE = 4;
GENETICS = 5;
ATTENTION_DISORDER = 6;
BORN_ON_EVEN_DAY = 7;
CAR_ACCIDENT = 8;
FATIGUE = 9;
ALLERGY = 10;
COUGHING = 11;
CANCER = 12;

dag(CANCER,[COUGHING ATTENTION_DISORDER FATIGUE]) = 1;
dag([FATIGUE ATTENTION_DISORDER], CAR_ACCIDENT) = 1;
dag([PEER_PRESSURE ANXIETY],SMOKING)=1;
dag(SMOKING,[YELLOW_FINGERS CANCER COUGHING])=1;
dag([GENETICS ALLERGY SMOKING BORN_ON_EVEN_DAY], CANCER)=1;
dag(GENETICS, ALLERGY)=1;


%define a vector of node sizes (all nodes discrete and binary)
discrete_nodes= 1:N;
node_sizes=2*ones(1,N);

%observed nodes? here:
observed_nodes=[];

 
data =load('Lung_Cancer.data');
labels = load('Lung_Cancer.targets');
trainData = [data, labels];
trainData=trainData+1;
trainData(trainData==0)=1;

%first read and match all to original data - why??
[dag trainData names] = fixTopology(dag, trainData, names);

%make the bayesian net
bnet=mk_bnet(dag, node_sizes, ...
    'discrete', discrete_nodes, ...
    'observed', observed_nodes, ...
    'names', names);



ncases = size(data, 1);
cases=cell(12, ncases);
cases(1:12,:)=num2cell(trainData');

seed = 0;
rand('state', seed);
for i=1:N
    bnet.CPD{i} = tabular_CPD(bnet, i);
end;

bnet = learn_params(bnet, cases);

CPT = cell(1,N);
for i=1:N
  s=struct(bnet.CPD{i});  % violate object privacy
  CPT{i}=s.CPT;
end
